unit uApontProd;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai_new, Mask, JvExMask, JvToolEdit, JvBaseEdits, StdCtrls,
  Menus, DB, Buttons, ExtCtrls, MMJPanel, FMTBcd, SqlExpr, DBClient,
  Provider, Grids, DBGrids, JvExDBGrids, JvDBGrid;

type
  TfApontProd = class(TfPai_new)
    sqlBusca: TSQLQuery;
    gridOf: TJvDBGrid;
    RefOf: TEdit;
    Label1: TLabel;
    RefProd: TEdit;
    Label2: TLabel;
    Label5: TLabel;
    edQtde: TJvCalcEdit;
    edProduto: TEdit;
    edOf: TEdit;
    Label3: TLabel;
    sqlRef: TSQLDataSet;
    dspRef: TDataSetProvider;
    cdsRef: TClientDataSet;
    dsRefugo: TDataSource;
    Label10: TLabel;
    edQtdeEmb: TEdit;
    BitBtn1: TBitBtn;
    procedure RefOfExit(Sender: TObject);
    procedure RefProdExit(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    function busca(sql: String): String;
    procedure buscaRefugo;
    { Private declarations }
  public
    codpro: string;
    { Public declarations }
  end;

var
  fApontProd: TfApontProd;

implementation

uses udm, uApontamento;

{$R *.dfm}

function TfApontProd.busca(sql: String): String;
begin
  if (sqlBusca.Active) then
    sqlBusca.Close;
  sqlBusca.SQL.Clear;
  sqlBusca.SQL.Add(sql);
  sqlBusca.Open;
  result := sqlBusca.Fields[0].AsString;
end;

procedure TfApontProd.RefOfExit(Sender: TObject);
begin
  if (RefOf.Text <> '') then
  begin
    edOf.Text := busca('SELECT DOF_CODIOF FROM ATS_OF, ATS_DETOF ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND DOF_CODIGO = ' +
      EdOf.Text);
    buscaRefugo;
    codpro := busca('SELECT COF_CODPRO FROM ATS_OF, ATS_DETOF ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND DOF_CODIGO = ' + EdOf.Text);
    edProduto.Text := busca('SELECT PRO_DESCRI ' +
      ' FROM ATS_OF, ATS_DETOF, F_PRODS ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND COF_CODPRO = PRO_CODPRO ' +
      ' AND DOF_CODIGO = ' +
      EdOf.Text);
  end;
end;

procedure TfApontProd.RefProdExit(Sender: TObject);
begin
  if (RefProd.Text <> '') then
    edProduto.Text := busca('SELECT PRO_DESCRI FROM F_PRODS WHERE PRO_CODPRO = ' +
      QuotedStr(RefProd.Text));
end;

procedure TfApontProd.buscaRefugo;
var tipo, sql: string;
begin
  tipo := '';
  tipo := busca('SELECT PRO_CODSUC FROM F_PRODS WHERE  PRO_CODPRO = ' +
    QuotedStr(refProd.Text));
  if (tipo = '') then // Refugo
  begin
    sql := 'SELECT RFG_DT_RFG DATA, RFG_QT_RFG QUANTIDADE, RFG_CODREF MOTIVO, ' +
      'RFG_LOTMAT LOTE FROM F_REFUGO WHERE RFG_CODIOF = ' +
      QuotedStr(edOf.Text);
  end
  else begin
    sql := 'SELECT OFS_DTLANC DATA, OFS_QTDPRO QUANTIDADE, OFS_CODMOT MOTIVO, ' +
      'OFS_LOTMAT LOTE FROM F_OFSUCATA WHERE OFS_CODIOF = ' +
      QuotedStr(edOf.Text);
  end;
  if (cdsRef.Active) then
    cdsRef.Close;
  cdsRef.CommandText := sql;
  cdsRef.Open;
end;

procedure TfApontProd.BitBtn1Click(Sender: TObject);
begin
  fApontamento.showModal;
end;

end.
